package com.newvpn.fastbest.zenith.activity;

import android.app.Dialog;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.newvpn.fastbest.zenith.VPNApp;
import com.newvpn.fastbest.zenith.dialog.LoadingDialog;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import ly.count.android.sdk.Countly;

public class BaseActivity extends FragmentActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //VPNApp.setInApp(true);
    }


    @Override
    protected void onStart() {
        Countly.sharedInstance().onStart(this);
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //VPNApp.setInApp(true);
    }

    @Override
    protected void onStop() {
        Countly.sharedInstance().onStop();
        super.onStop();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        Countly.sharedInstance().onConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
    }

    private Dialog loadingDialog;

    private Disposable disposable;

    public void showLoading() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
            loadingDialog.setCancelable(false);
        }
        loadingDialog.setCancelable(false);
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
        if (disposable != null) {
            disposable.dispose();
        }
        Observable.timer(15, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread()) // 指定在主线程上执行
                .subscribe(
                        aLong -> {

                        },
                        throwable -> {
                        },
                        () -> {
                            loadingDialog.setCancelable(true);
                        }
                );
    }

    public void endLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        endLoading();
    }
}
